package com.ldgame.gfx.gfxelements;

import static org.lwjgl.opengl.GL11.*;
import java.util.ArrayList;
import org.newdawn.slick.Color;
import org.lwjgl.util.vector.Vector2f;

public class Sprite implements GfxComponent {
	protected Vector2f position;
	private Vector2f camPosition = new Vector2f(400.0f, 300.0f);
	private float width;
	private float height;
	protected Vector2f texCoord;
	private float texOffset = 1.0f;
	private Vector2f texSize;
	private Color color;
	
	private int animFrames = 1;
	private int currentFrame = 0;
	private float animTimer = 0.0f;
	private float frameTime = 0.5f;
	private int spriteState = 0;

	public Sprite(Vector2f position, float width, float height, Vector2f texCoord, Vector2f texSize, Color color) {
		this.position = position;
		this.width = width;
		this.height = height;
		this.texCoord = texCoord;
		this.texSize = texSize;
		this.color = color;
	}
	
	public Sprite(Vector2f position, float width, float height, Vector2f texCoord, Vector2f texSize, Color color, int animFrames, float frameTime) {
		this.position = position;
		this.width = width;
		this.height = height;
		this.texCoord = texCoord;
		this.texSize = texSize;
		this.color = color;
		this.animFrames = animFrames;
		this.frameTime = frameTime;
	}
	
	@Override
	public Vector2f getPosition() {
		return position;
	}

	@Override
	public void setPosition(Vector2f position) {
		this.position = position;
	}

	public void setTexCoord(Vector2f texCoord) {
		this.texCoord = texCoord;
	}
	
	public void setTexSize(Vector2f texSize) {
		this.texSize = texSize;
	}
	
	@Override
	public float getWidth() {
		return width;
	}
	
	@Override
	public void setWidth(float width) {
		this.width = width;
	}

	@Override
	public float getHeight() {
		return height;
	}
	
	@Override
	public void setHeight(float height) {
		this.height = height;
	}

	@Override
	public Color getColor() {
		return color;
	}

	@Override
	public void setColor(Color color) {
		this.color = color;
	}
	
	public float getTexOffset() {
		return texOffset;
	}

	public void setTexOffset(float texOffset) {
		this.texOffset = texOffset;
	}

	public void setAnimFrames(int animFrames) {
		this.animFrames = animFrames;
	}
	
	public void setAnimTimer(float animTimer) {
		this.animTimer = animTimer;
	}
	
	public int getSpriteState() {
		return spriteState;
	}

	public void setSpriteState(int spriteState) {
		this.spriteState = spriteState;
	}

	public Vector2f getCamPosition() {
		return camPosition;
	}

	public void setCamPosition(Vector2f camPosition) {
		this.camPosition = camPosition;
	}

	public void sync(float loopTime) {
		animTimer += loopTime;
		
		if (animTimer > frameTime) {
			if (currentFrame < animFrames - 1) {
				++currentFrame;
			} else {
				currentFrame = 0;
			}
			
			animTimer = 0.0f;
		}
	}
	
	@Override
	public void render() {
		float relPosX = position.x - camPosition.x + 400.0f;
		float relPosY = position.y - camPosition.y + 300.0f;
		
		if (relPosX > -128.0f && relPosX < 928.0f &&
			relPosY > -128.0f && relPosY < 728.0f)
		{
			glColor4f(color.r, color.g, color.b, color.a);
			glPushMatrix();
			glTranslatef(relPosX, relPosY, 0.0f);
			glScalef(width, height, 1.0f);
			
			glBegin(GL_TRIANGLE_STRIP);
				glTexCoord2f((texCoord.x + texOffset + texSize.x * (float)currentFrame) / 1024.0f,
							 (texCoord.y + texSize.y - texOffset + texSize.y * (float)spriteState) / 1024.0f);
				glVertex3f(-0.5f, 0.5f, 0.0f);
				
				glTexCoord2f((texCoord.x + texSize.x - texOffset + texSize.x * (float)currentFrame) / 1024.0f,
							 (texCoord.y + texSize.y - texOffset + texSize.y * (float)spriteState) / 1024.0f);
				glVertex3f(0.5f, 0.5f, 0.0f);
				
				glTexCoord2f((texCoord.x + texOffset + texSize.x * (float)currentFrame) / 1024.0f,
						 	 (texCoord.y + texOffset + texSize.y * (float)spriteState) / 1024.0f);
				glVertex3f(-0.5f, -0.5f, 0.0f);
				
				glTexCoord2f((texCoord.x + texSize.x - texOffset + texSize.x * (float)currentFrame) / 1024.0f,
							 (texCoord.y + texOffset + texSize.y * (float)spriteState) / 1024.0f);
				glVertex3f(0.5f, -0.5f, 0.0f);
			glEnd();
			
			glPopMatrix();
		}
	}

	@Override
	public boolean add(GfxComponent component) {
		return false;
	}

	@Override
	public boolean remove(GfxComponent component) {
		return false;
	}

	@Override
	public GfxComponent getChild(int i) {
		return null;
	}

	@Override
	public ArrayList<GfxComponent> getComponents() {
		return null;
	}
}
